package com.bjsxt.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.controller.BaseController;
import com.bjsxt.common.util.ConstantFinal;
import com.bjsxt.common.util.DateFormatUtil;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.investor.pojo.Investor;
import com.bjsxt.investor.service.IInvestorService;

@Controller
@RequestMapping("/back/investor/")
public class InvestorBackController extends BaseController
{
	@Resource
	private IInvestorService investorService;
	
	@RequestMapping(value="/investorUpdateSubmit.htm",produces="text/html;charset=utf-8")
	public @ResponseBody String investorUpdateSubmit(String id,HttpServletRequest req,MultipartFile file)
	{
		ConstantFinal.LOGGER_SYS.info("==investorUpdateSubmint==");
		Map<String, Object> condMap = new HashMap<String,Object>();
		condMap.put("id", id);
		Investor investor = this.investorService.findOneInvestor(condMap);
		if(investor!=null)
		{
			//接收要修改的参数
			String code = req.getParameter("code");
			String name = req.getParameter("name");
			String url = req.getParameter("url");
			String contactName = req.getParameter("contactName");
			String contactPhone = req.getParameter("contactPhone");
			String content = req.getParameter("content");
			String totalNum = req.getParameter("totalNum");
			String succedNum = req.getParameter("succedNum");
			String coopType = req.getParameter("coopType");
			String status = req.getParameter("status");
			
			//将要修改的参数设置到investor中
			investor.setName(contactName);
			investor.setCode(code);
			investor.setUrl(url);
			investor.setContactName(contactName);
			investor.setContactPhone(contactPhone);
			investor.setContent(content);
			investor.setTotalNum(Integer.valueOf(totalNum));
			investor.setSuccedNum(Integer.valueOf(succedNum));
			investor.setCoopType(Byte.valueOf(coopType));
			investor.setStatus(Byte.valueOf(status));
			investor.setUpdateTime(new Date());
			
			//处理文件上传
			Map<String ,Object> paramMap=new HashMap<String,Object>();
			
			String realPath = req.getRealPath("/");
			paramMap.put("realPath", realPath);
			if(file!=null && !"".equalsIgnoreCase(file.getOriginalFilename()))
			{
				paramMap.put("file", file);
			}
			//更新投资方信息
			String info = "";
			try
			{
				JSONObject updateJson = this.investorService.updateOneInvestor(investor,paramMap);
				info = updateJson.getString("info");
			} catch (Exception e)
			{
				info = "geng xin shi bai le "; 
			}
			JSONObject juiNeedJson = this.returnJson(req, info);
			return juiNeedJson.toJSONString();
		}else
		{
			String info="要更新的投资方不存在";
			JSONObject juiNeedJson = this.returnJson(req, info);
			return juiNeedJson.toJSONString();
		}
	}
	@RequestMapping("investorUpdate.htm")
	public String investorUpdate(String id,HttpServletRequest req)
	{
		ConstantFinal.LOGGER_SYS.info("==investorUpdate==");
		Map<String, Object> condMap = new HashMap<String,Object>();
		condMap.put("id", id);
		Investor investor = this.investorService.findOneInvestor(condMap );
		req.setAttribute("investor", investor);
		req.setAttribute("id", id);
		
		return "/back/investor/investorUpdate";
	}
	
	//打开出资方列表页面
	@RequestMapping("investorList.htm")
	public String investorList(HttpServletRequest req)
	{
		ConstantFinal.LOGGER_SYS.info("==investorList===");
		PageInfoUtil pageInfo = this.getPageInfoUtil(req);
		
		//接收查找条件
		String keyword = req.getParameter("keyword");
		String status = req.getParameter("status");
		String coopType = req.getParameter("coopType");
		String st = req.getParameter("st");
		String ed = req.getParameter("ed");
		
		Map<String, Object> condMap=new HashMap<String,Object>();
		condMap.put("keyword", keyword);
		condMap.put("status", status);
		condMap.put("coopType", coopType);
		
		Date stDate=null;
		Date edDate=null;
		if(st!=null && ed!=null)
		{
			stDate = DateFormatUtil.parseIntoDateTime(st);
			edDate = DateFormatUtil.parseIntoDateTime(ed);
			condMap.put("stDate", stDate);
			condMap.put("edDate", edDate);
		}
		
		
		
		List<Investor> investorList = this.investorService.findListInvestor(pageInfo, condMap);
		
		req.setAttribute("investorList", investorList);
		req.setAttribute("pageInfo", pageInfo);
		req.setAttribute("keyword", keyword);
		req.setAttribute("status", status);
		req.setAttribute("coopType", coopType);
		req.setAttribute("st", st);
		req.setAttribute("ed", ed);
		return "/back/investor/investorList";
		
	}
}
